# Appendix C
# Santiago Lopez-Cariboni, 2022. "Political Regimes and Informal Social Insurance", Comparative Political Studies.

# ---- wd ----
wd <- 'replication_materials' 
setwd(wd)

# ---- packages ----
pkgs <- c("data.table", "foreign", 'panelView', 'fect', "lubridate", 
    "ggplot2", "dplyr", "mFilter", "texreg", "AER", "xts", "interflex", "gridExtra")
suppressPackageStartupMessages(sapply(pkgs,require,character.only=T))
# Plot the effects (use Johannes Karreth's function: https://github.com/jkarreth/JKmisc/blob/master/ggintfun.R)
# devtools::source_url("https://raw.githubusercontent.com/jkarreth/JKmisc/master/ggintfun.R")
source('code/functions/ggintfun.R')
# Function for time Lags
source('code/functions/lagpanel.R')


# ---- data ----
dt <- fread('data/dt_replication.csv')
dt <- dt[LDC==1, ]

# ---- Table 2 ----
m.demo <- lm(elec.access.total ~  
    + outgap.tdl 
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==1,], na.action=na.omit)
summary(m.demo,  diagnostics = TRUE)
nc4.demo <- length(unique(m.demo$model[['as.factor(iso3c)']]))

m.auto <- lm(elec.access.total ~ 
    + outgap.tdl 
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==0,], na.action=na.omit)
summary(m.auto,  diagnostics = TRUE)
nc4.auto <- length(unique(m.auto$model[['as.factor(iso3c)']]))


m <- lm(elec.access.total ~  
    + outgap.tdl*democracy
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m,  diagnostics = TRUE)
nc4 <- length(unique(m$model[['as.factor(iso3c)']]))


m.controls <- lm(elec.access.total ~  
    + outgap.tdl * democracy
    + l.capacity
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
summary(m.controls,  diagnostics = TRUE)
nc4.controls <- length(unique(m.controls$model[['as.factor(iso3c)']]))



v.names <- c(
    "Electricity Losses Cycle",
    "Democracy$_{t-1}$",
    "Democracy$_{t-1}$ $\\times$ Losses Cycle",
    "State Capacity$_{t-1}$",
    "Imports$_{t-1}$",
    "Exports$_{t-1}$",
    "Population (log)$_{t-1}$",
    "Real GDP per capita (log)$_{t-1}$",
    "Population Density$_{t-1}$"
    )



table1 <- texreg(list(m.demo, m.auto, m, m.controls),
    file= "tables/OA/T2_losses_access.tex",
    label="tab:losses_access",
    custom.model.names = c('(Democracies)','(Autocracies)', '(3)', '(4)'),
    caption="Effects of Electricity Losses on Access to Electricity in Autocracies and Democracies",
    dcolumn = TRUE,
    no.margin=FALSE,
    fontsize="scriptsize",
    single.row=FALSE,
    use.packages=FALSE,
    booktabs = TRUE,
    digits=2,
    float.pos="htbp",
    sideways=FALSE,
    omit.coef= "(year)|(iso3c)|(Intercept)",
    include.rsquared = FALSE,
    stars = c(0.01, 0.05, 0.1),
    custom.gof.rows = list("Year dummies" = c("Yes", "Yes", "Yes", "Yes"), 
        "Country fixed-effects" = c("Yes", "Yes", "Yes", "Yes"),
        "Number of countries"= c(nc4.demo, nc4.auto, nc4, nc4.controls)),
    # reorder.gof = c(1, 2, 5, 3, 4),
    custom.coef.names=v.names
    )